﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DistributionRangeBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDistributionRangeDA distributionRangeDA = DataAccess.CreateDistributionRangeDA();
        private PubParasInfo pubParasInfo;
        private static readonly IVendorDA vendorDA = DataAccess.CreateVendorDA();

        public DistributionRangeBLL()
        {
            this.pubParasInfo = null;
        }

        public DistributionRangeBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DistributionRangeBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> distributionRangeInfoIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                distributionRangeDA.DeleteDistributionRange(dataBase, tran, distributionRangeInfoIDS, this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public string DistributionRangeImportByData(string[][] ImportByData)
        {
            Exception ex;
            try
            {
                int i;
                string whLoginID = this.dataBaseID[0];
                List<DistributionRangeInfo> DistributionRangeInfoList = new List<DistributionRangeInfo>();
                for (i = 1; i < ImportByData.Length; i++)
                {
                    DistributionRangeInfo distributionRangeInfo = new DistributionRangeInfo {
                        CreatedBy = this.pubParasInfo.UserID,
                        UpdatedBy = this.pubParasInfo.UserID
                    };
                    for (int j = 0; j < ImportByData[i].Length; j++)
                    {
                        switch (ImportByData[0][j].ToUpper().Trim())
                        {
                            case "省份":
                            {
                                distributionRangeInfo.Province = ImportByData[i][j];
                                continue;
                            }
                            case "城市":
                            {
                                distributionRangeInfo.City = ImportByData[i][j];
                                continue;
                            }
                            case "区县":
                            {
                                distributionRangeInfo.Counties = ImportByData[i][j];
                                continue;
                            }
                            case "承运商":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    break;
                                }
                                distributionRangeInfo.CarrierID = ImportByData[i][j];
                                continue;
                            }
                            case "首重重量":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_01C3;
                                }
                                distributionRangeInfo.InitialWeight = decimal.Parse(ImportByData[i][j]);
                                continue;
                            }
                            case "首重价格":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_01FE;
                                }
                                distributionRangeInfo.InitialWeightPrice = decimal.Parse(ImportByData[i][j]);
                                continue;
                            }
                            case "续重重量":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_0239;
                                }
                                distributionRangeInfo.ContinuousWeight = decimal.Parse(ImportByData[i][j]);
                                continue;
                            }
                            case "续重价格":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_0271;
                                }
                                distributionRangeInfo.ContinuousWeightPrice = decimal.Parse(ImportByData[i][j]);
                                continue;
                            }
                            case "备选承运商":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][j]))
                                {
                                    goto Label_02A4;
                                }
                                distributionRangeInfo.AlternateCarrierID = ImportByData[i][j];
                                continue;
                            }
                            default:
                            {
                                continue;
                            }
                        }
                        distributionRangeInfo.CarrierID = "";
                        continue;
                    Label_01C3:
                        distributionRangeInfo.InitialWeight = 0M;
                        continue;
                    Label_01FE:
                        distributionRangeInfo.InitialWeightPrice = 0M;
                        continue;
                    Label_0239:
                        distributionRangeInfo.ContinuousWeight = 0M;
                        continue;
                    Label_0271:
                        distributionRangeInfo.ContinuousWeightPrice = 0M;
                        continue;
                    Label_02A4:
                        distributionRangeInfo.AlternateCarrierID = "";
                    }
                    DistributionRangeInfoList.Add(distributionRangeInfo);
                }
                if ((DistributionRangeInfoList != null) && (DistributionRangeInfoList.Count > 0))
                {
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        for (i = 0; i < DistributionRangeInfoList.Count; i++)
                        {
                            distributionRangeDA.InsertDistributionRange(dataBase, tran, DistributionRangeInfoList[i], whLoginID);
                        }
                        tran.Commit();
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        tran.Rollback();
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return distributionRangeDA.GetAllDistributionRange(this.dataBaseID[0]);
        }

        public object GetInfoByID(string distributionRangeInfoID)
        {
            return distributionRangeDA.GetDistributionRangeByID(distributionRangeInfoID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            DistributionRangeQueryEntity distributionRangeQueryEntity = obj as DistributionRangeQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(distributionRangeQueryEntity.Province))
            {
                sqlWhere.Add(" Province " + distributionRangeQueryEntity.Province);
            }
            if (!string.IsNullOrEmpty(distributionRangeQueryEntity.City))
            {
                sqlWhere.Add(" City " + distributionRangeQueryEntity.City);
            }
            if (!string.IsNullOrEmpty(distributionRangeQueryEntity.Counties))
            {
                sqlWhere.Add(" COUNTIES " + distributionRangeQueryEntity.Counties);
            }
            if (!string.IsNullOrEmpty(distributionRangeQueryEntity.Counties))
            {
                sqlWhere.Add(" COUNTIES " + distributionRangeQueryEntity.Counties);
            }
            if (!string.IsNullOrEmpty(distributionRangeQueryEntity.CarrierID))
            {
                sqlWhere.Add(" CARRIER_ID " + distributionRangeQueryEntity.CarrierID);
            }
            return distributionRangeDA.GetDistributionRangeByQueryList(sqlWhere, distributionRangeQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            return "";
        }

        public string Update(object obj)
        {
            string distributionRangeInfoID = string.Empty;
            try
            {
                DistributionRangeInfo distributionRangeInfo = obj as DistributionRangeInfo;
                if (!string.IsNullOrEmpty(distributionRangeInfo.CarrierID) && !vendorDA.CheckVendorIDUnique(distributionRangeInfo.CarrierID, this.dataBaseID[0]))
                {
                    throw new Exception("供应商" + distributionRangeInfo.CarrierID + "不存在!");
                }
                if (!string.IsNullOrEmpty(distributionRangeInfo.AlternateCarrierID) && !vendorDA.CheckVendorIDUnique(distributionRangeInfo.AlternateCarrierID, this.dataBaseID[0]))
                {
                    throw new Exception("供应商" + distributionRangeInfo.AlternateCarrierID + "不存在!");
                }
                distributionRangeInfo.UpdatedDate = DateTime.Now;
                distributionRangeInfo.UpdatedBy = this.pubParasInfo.UserID;
                distributionRangeDA.UpdateDistributionRange(distributionRangeInfo, this.dataBaseID[0]);
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return distributionRangeInfoID;
        }
    }
}

